import datetime
import requests
import time
from constants import *
from requestor import StatRequestor
from stats import Stats

class App:
	"""The App class is holding information about the mobile app"""
	name = ""
	platform = ""
	api_key = ""

	stats = None

	def __init__(self, name, platform, apikey):
		self.name = name
		self.platform = platform
		self.api_key = apikey

	def get_app_stats(self, report):
		
		p = {'apiAccessCode' : APIACCESSCODE, 'apiKey' : self.api_key, 'startDate' : report.start_date, 'endDate' : report.end_date}
		requestor = StatRequestor("appMetrics/", "Sessions", p)

		print "Setting stats for app:"
		print self.name
		print "Stats are now:"
		print self.stats
		print "and now"
		self.stats = requestor.get_stats()
		print self.stats
	#	print(self.stats)
		
#		print "Retrieving sessions for app: " + self.name + " : " + self.platform

#		sDate = datetime.date(2012, 12, 30)
#		eDate = datetime.date(2013, 12, 24)
#		week = sDate.isocalendar()[1]
#		key = str(sDate.isocalendar()[0]) + '-' + str(sDate.isocalendar()[1])
#		stats = dict()


#		r = requests.get('http://api.flurry.com/appMetrics/Sessions', params=p)

#		print r.url
#		print r.status_code
#		print r.json()

#		days = r.json()
#		sessions_sum = int(0)
#		for day in days['day']:
#			wDate = setDateByIsoString(day['@date'])
#			currentWeek = wDate.isocalendar()[1]
#			if week == wDate.isocalendar()[1]:
#				key = str(wDate.isocalendar()[0]) + '-' + padWeekNo(str(wDate.isocalendar()[1]))
#				sessions_sum = sessions_sum + int(day['@value'])
#			else:
#				stats[key] = sessions_sum
#				sessions_sum = int(day['@value'])
#				week = currentWeek

#			print "Day " + 	day['@date'] + " : Sessions : " + day['@value']

#		print collections.OrderedDict(sorted(stats.items()))
#		exit()

#		return stats


	def setDateByIsoString(str):
		d = str.split('-')
		return datetime.date(int(d[0]), int(d[1]), int(d[2]))

	def padWeekNo(str):
		if len(str) <= 1:
			str = '0' + str

		return str
	def __str__(self):
		return "App (%s, %s, %s, %s)" % (self.name, self.platform, self.api_key, self.stats)

	def __repr__(self):
		return "App (%s, %s, %s, %s)" % (self.name, self.platform, self.api_key, self.stats)


if __name__ == "__main__":
	pass
